//
//  YSHomeTableViewCell.swift
//  新浪浪
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit



class YSHomeTableViewCell: UITableViewCell {
    
    var constant: Constraint?
    
    var statusViewModel :YSStatusViewModel? {
        
        didSet{
            originalView.statusViewModel = statusViewModel
            footerView.footViewModel = statusViewModel
            
            constant?.uninstall()
            
            if statusViewModel?.statusModel?.retweeted_status == nil {
                retweetView.hidden = true
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }else {
                retweetView.hidden = false
                retweetView.retweetViewModel = statusViewModel
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    
    private func setupUI(){
        contentView.addSubview(originalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
//            make.bottom.equalTo(contentView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
            
//            self.constant = make.bottom.equalTo(retweetView.snp_top).constraint
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
        }
        
        
        
        footerView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(originalView.snp_bottom)
            self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
            make.height.equalTo(40)
        }
        
        //把视图放置到 contentView
        //contentView是根据自动计算的
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(footerView.snp_bottom)
        }
        
    }
    
    
    private lazy var originalView:YSOriginalView = YSOriginalView()
    private lazy var footerView:YSFooterView = YSFooterView()
    private lazy var retweetView:YSRetweet = YSRetweet()
    
    

}
